﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FancyBattles.GameAudio;
using Microsoft.Xna.Framework.Graphics;

namespace FancyBattles.GameScreen
{
    class MainMenuScreen : GameScreen
    {
        #region Atributes

        public static LinkedList<MenuEntry> mainMenuEntries = new LinkedList<MenuEntry>();
        public static MenuEntry singlePlayerEntry;
        public static MenuEntry multiPlayerEntry;
        public static MenuEntry creditsEntry;
        public static MenuEntry tutorialEntry;
        private MenuEntry currentMenuEntry;

        public static Texture2D mainMenuBG;
        
        #endregion

        #region Properties
        #endregion

        #region Creators
        public MainMenuScreen()
        {
            
        }
        #endregion

        #region Methods

        public override void LoadContent()
        {            
            base.LoadContent();

            //AudioManager.PlayMusicLoop("explosion");
            mainMenuBG = ContentManager.Load<Texture2D>("Screens/MENU_NOT_SELECTED");
            
            singlePlayerEntry = new MenuEntry(ContentManager.Load<Texture2D>("Screens/MENU_SINGLE_SELECTED"));
            multiPlayerEntry = new MenuEntry(ContentManager.Load<Texture2D>("Screens/MENU_MULTI_SELECTED"));
            creditsEntry = new MenuEntry(ContentManager.Load<Texture2D>("Screens/MENU_CREDITS_SELECTED")); ;
            tutorialEntry = new MenuEntry(ContentManager.Load<Texture2D>("Screens/MENU_HELP_SELECTED")); ;

            mainMenuEntries.AddLast(singlePlayerEntry);
            mainMenuEntries.AddLast(multiPlayerEntry);
            mainMenuEntries.AddLast(tutorialEntry);
            mainMenuEntries.AddLast(creditsEntry);

            singlePlayerEntry.IsSelected = true;
            currentMenuEntry = singlePlayerEntry;
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        public override UpdateResponse Update(GameTime gameTime)
        {           
            UpdateResponse updateAnswer = UpdateResponse.ERROR;
            if (InputHandler.isConfirm(PlayerIndex.One))
            {
                updateAnswer = UpdateResponse.CLOSE_SCREEN;
            }
            if (InputHandler.isCancel(PlayerIndex.One))
            {
                updateAnswer = UpdateResponse.EXIT_GAME;
            }
            if (InputHandler.isMenuUp(PlayerIndex.One))
            {
                if (currentMenuEntry.Equals(mainMenuEntries.First.Value))
                {
                    singlePlayerEntry.IsSelected = false;
                    creditsEntry.IsSelected = true;
                    currentMenuEntry = creditsEntry;
                }
                else
                {
                    currentMenuEntry.IsSelected = false;
                    currentMenuEntry = mainMenuEntries.Find(currentMenuEntry).Previous.Value;
                    currentMenuEntry.IsSelected = true;
                }
                updateAnswer = UpdateResponse.KEEP_STATE;
            }
            if (InputHandler.isMenuDown(PlayerIndex.One))
            {
                if (currentMenuEntry.Equals(mainMenuEntries.Last.Value))
                {
                    creditsEntry.IsSelected = false;
                    singlePlayerEntry.IsSelected = true;
                    currentMenuEntry = singlePlayerEntry;
                }
                else
                {
                    currentMenuEntry.IsSelected = false;
                    currentMenuEntry = mainMenuEntries.Find(currentMenuEntry).Next.Value;
                    currentMenuEntry.IsSelected = true;
                }
                updateAnswer = UpdateResponse.KEEP_STATE;
            }
            return updateAnswer;
        }

        public override void Draw(GameTime gameTime)
        {           
            Color startGameColor = Color.White;
            Color exitGameColor = Color.White;

            Rectangle fullscreen = new Rectangle(0, 0, 1280, 720);

            SpriteBatch.Begin();

            //Draw MainMenuScreen
            ScreenManager.SpriteBatch.Draw(mainMenuBG, fullscreen, Color.White);

            //Draw Option Selected
            ScreenManager.SpriteBatch.Draw(currentMenuEntry.MenuSelected, fullscreen, Color.White);

            if (singlePlayerEntry.IsSelected)
            {
                startGameColor = Color.Yellow;
            }
            else
            {
                exitGameColor = Color.Yellow;
            }

            SpriteBatch.End();
            base.Draw(gameTime);            
        }

        #endregion
    }
}
